package org.jsola.hr.query;

import io.swagger.annotations.ApiParam;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.hibernate.validator.constraints.Length;
import org.jsola.core.query.BaseQuery;
import org.jsola.orm.filter.Order;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;

import static org.jsola.core.example.BaseExample.E_CREATE_USER_ID;
import static org.jsola.core.example.BaseExample.E_MODIFIED_USER_ID;
import static org.jsola.core.example.BaseExample.N_CREATE_USER_ID;
import static org.jsola.core.example.BaseExample.N_MODIFIED_USER_ID;
import static org.jsola.hr.example.SubsidyProgExample.E_ATTENDANCE_RANGE;
import static org.jsola.hr.example.SubsidyProgExample.E_ATTENDANCE_RANGE_DAYS;
import static org.jsola.hr.example.SubsidyProgExample.E_COMPANY_ID;
import static org.jsola.hr.example.SubsidyProgExample.E_GMT_CREATE_END;
import static org.jsola.hr.example.SubsidyProgExample.E_GMT_CREATE_START;
import static org.jsola.hr.example.SubsidyProgExample.E_GMT_MODIFIED_END;
import static org.jsola.hr.example.SubsidyProgExample.E_GMT_MODIFIED_START;
import static org.jsola.hr.example.SubsidyProgExample.E_NAME;
import static org.jsola.hr.example.SubsidyProgExample.E_REMARK;
import static org.jsola.hr.example.SubsidyProgExample.E_TAX_METHOD;
import static org.jsola.hr.example.SubsidyProgExample.N_ATTENDANCE_RANGE;
import static org.jsola.hr.example.SubsidyProgExample.N_ATTENDANCE_RANGE_DAYS;
import static org.jsola.hr.example.SubsidyProgExample.N_COMPANY_ID;
import static org.jsola.hr.example.SubsidyProgExample.N_GMT_CREATE_END;
import static org.jsola.hr.example.SubsidyProgExample.N_GMT_CREATE_START;
import static org.jsola.hr.example.SubsidyProgExample.N_GMT_MODIFIED_END;
import static org.jsola.hr.example.SubsidyProgExample.N_GMT_MODIFIED_START;
import static org.jsola.hr.example.SubsidyProgExample.N_NAME;
import static org.jsola.hr.example.SubsidyProgExample.N_REMARK;
import static org.jsola.hr.example.SubsidyProgExample.N_TAX_METHOD;

/**
 * @author zhr
 */
@EqualsAndHashCode(callSuper = true)
@Data
public class SubsidyProgQuery extends BaseQuery {

    private static final long serialVersionUID = 1L;

    @ApiParam(value = N_NAME, example = E_NAME)
    @Length(max = 255)
    private String name;

    @ApiParam(value = N_REMARK, example = E_REMARK)
    @Length(max = 255)
    private String remark;

    @ApiParam(value = N_TAX_METHOD, example = E_TAX_METHOD)
    private Integer taxMethod;

    @ApiParam(value = N_ATTENDANCE_RANGE, example = E_ATTENDANCE_RANGE)
    private Boolean attendanceRange;

    @ApiParam(value = N_ATTENDANCE_RANGE_DAYS, example = E_ATTENDANCE_RANGE_DAYS)
    private Integer attendanceRangeDays;

    @ApiParam(value = N_COMPANY_ID, example = E_COMPANY_ID)
    @Length(max = 32)
    private String companyId;

    /**
     * 创建时间查询范围
     */
    @ApiParam(value = N_GMT_CREATE_START, example = E_GMT_CREATE_START)
    private Date gmtCreateStart;
    @ApiParam(value = N_GMT_CREATE_END, example = E_GMT_CREATE_END)
    private Date gmtCreateEnd;


    @ApiParam(value = N_CREATE_USER_ID, example = E_CREATE_USER_ID)
    @Length(max = 32)
    private String createUserId;

    /**
     * 修改时间查询范围
     */
    @ApiParam(value = N_GMT_MODIFIED_START, example = E_GMT_MODIFIED_START)
    private Date gmtModifiedStart;
    @ApiParam(value = N_GMT_MODIFIED_END, example = E_GMT_MODIFIED_END)
    private Date gmtModifiedEnd;


    @ApiParam(value = N_MODIFIED_USER_ID, example = E_MODIFIED_USER_ID)
    @Length(max = 32)
    private String modifiedUserId;



    @ApiParam(value = "排序字段", allowMultiple=true, type="Order" ,example = "\"orders\": [{\"property\": \"userName\",\"desc\": false}]")
    private List<Order> orders;

    /**
     * 每页的条数
     */
    @ApiParam(value = "分页参数，每页的条数", example = "20")
    @Max(value = 1000, message = "pageSize不能大于1000")
    private Integer pageSize = 20;

    /**
     * 当前第几页
     */
    @ApiParam(value = "分页参数，第几页", example = "1")
    @Min(value = 1, message = "pageNo不能小于1")
    private Integer pageNo = 1;



    /**
     * 添加排序
     * @param propertyName 属性名
     * @param desc 是否降序
     * @return this
     */
    public SubsidyProgQuery addOrder(String propertyName, boolean desc) {
        if (CollectionUtils.isEmpty(this.orders)) {
            this.orders = new ArrayList<>();
        }
        this.orders.add(new Order(propertyName,desc));
        return this;
    }

}

